<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <script type="text/javascript" src="js/plugins/d3/d3.v4.js"></script>
    <script type="text/javascript" src="js/plugins/jquery/jquery-1.9.1.js"></script>
    <link rel="stylesheet" href="css/demo.css"/>
    <style>
        .node {
            font: 10px sans-serif;
        }
        .link {
            stroke: steelblue;
            stroke-opacity: 0.5;
            fill: none;
            pointer-events: none;
        }
    </style>
</head>
<body>
<svg width="960" height="500"></svg>

<script type="text/javascript">
    var svg = d3.select("svg"),
            width = +svg.attr("width"),
            height = +svg.attr("height"),
            g = svg.append("g").attr("transform", "translate(40,0)");

    var tree = d3.cluster().size([height, width - 160]); //集群图
    //var tree = d3.tree().size([height, width - 160]); 树形图

    d3.json("js/data/cluster.json", function (error, data) {
        if (error) throw error;
        var root = d3.hierarchy(data);
        tree(root);
        var link = g.selectAll(".link")
                .data(root.descendants().slice(1))
                .enter().append("path")
                .attr("class", "link")
                .attr("d", function (d) {
                    return "M" + d.y + "," + d.x
                            + "C" + (d.parent.y + 100) + "," + d.x
                            + " " + (d.parent.y + 100) + "," + d.parent.x
                            + " " + d.parent.y + "," + d.parent.x;
                });
        var node = g.selectAll(".node")
                .data(root.descendants())
                .enter().append("g")
                .attr("class", function (d) {
                    return "node" + (d.children ? " node--internal" : " node--leaf");
                })
                .attr("transform", function (d) {
                    return "translate(" + d.y + "," + d.x + ")";
                });
        node.append("circle")
                .attr("r", 2.5);
        node.append("text")
                .attr("dy", 3)
                .attr("x", function (d) {
                    return d.children ? -8 : 8;
                })
                .style("text-anchor", function (d) {
                    return d.children ? "end" : "start";
                })
                .text(function (d) {
                    return d.data.name
                });
    });

</script>
</body>
</html>